const {app, BrowserWindow, screen} = require('electron')
const path = require('path')
const url = require('url');

let win

const createWindow = () => {
  const size = screen.getPrimaryDisplay().workAreaSize;
  win = new BrowserWindow({
    x: 0,
    y: 0,
    width: size.width,
    height: size.height,
    webPreferences: {
      nodeIntegration: true
    },
  })

  // 移除菜单
  win.removeMenu();
  // win.webContents.openDevTools();

  win.loadURL(url.format({
    pathname: path.join(__dirname, "dist/index.html"),
    protocol: 'file:',
    slashes: true
  }));

  win.webContents.on('did-fail-load', () => {
    win.loadURL(url.format({
      pathname: path.join(__dirname, 'dist/index.html'),
      protocol: 'file:',
      slashes: true
    }));
  });

  win.on('closed', () => {
    win = null;
  });

  return win;
}

try {

  app.on('ready', createWindow);

  app.on('window-all-closed', () => {
    if (process.platform !== 'darwin') {
      app.quit();
    }
  });

  app.on('activate', () => {
    if (win === null) {
      createWindow();
    }
  });

} catch (e) {
  // Catch Error
  // throw e;
}
